TableView表视图的使用 |
您所在的位置:网站首页 › axure rp8打不开文件 › TableView表视图的使用 |
1、普通表视图的使用
表视图的几个协议:
UITableViewDataSource /设定表格有多少行 -(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section; //设定每一行有什么内容 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath; //设定表格有几段 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //设定每一段的标题 - (NSString*)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section; //设定表格的索引的内容 - (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView;
如下代码 -(void)loadView{ UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 20, [UIScreenmainScreen].applicationFrame.size.width, [UIScreenmainScreen].applicationFrame.size.height)]; self.view = view; //实例化tableView 并且设定表格样式 UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.framestyle:UITableViewStylePlain]; //设定背景颜色 tableView.backgroundColor = [UIColororangeColor]; //设定分割线的颜色,设定线的类型 tableView.separatorColor = [UIColorblueColor]; //tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLineEtched; //设定表格的代理 tableView.dataSource = self; tableView.delegate = self; //设定表格的每一行的行高 //tableView.rowHeight = 100; [self.view addSubview:tableView]; //给dataArr赋值 dataArr = [[UIFont familyNames] retain]; imageArr = [[NSArray alloc]initWithObjects:@"weige.jpg",@"weige.png", nil]; //NSLog(@"dataArr : %@",dataArr); } //实现方法 .m文件 设置表格的行数 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//返回数组的长度,有多少个元素,就显示多少行 return dataArr.count; }
//每个单元格显示的内容 tableView把当前的表视图返回了,每一行索引 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ //定义标志 static NSString *reuseFlag = @"xxx"; //根据标志从内存中获取是否可以重用(被拽上去不显示的那些cell)的cell UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:reuseFlag]; //判断是否获取到,如果获取到,返回值是cell,如果获取不到,返回值nil if (cell == nil) { //如果获取不到,则需要实例化cell cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseFlag]autorelease]; } //设置cell的内容 cell.textLabel.font = [UIFontfontWithName:[dataArr objectAtIndex:indexPath.row] size:18.0]; //设定文字 cell.textLabel.text = [dataArrobjectAtIndex:indexPath.row]; //设定单元格的图片 //cell.imageView.image = [UIImageimageNamed:[imageArr objectAtIndex:indexPath.row]]; cell.imageView.image = [UIImageimageNamed:@"weige.jpg"]; return cell; } UITableViewDelegate
//监听单元格选中事件(单元格选中后就会执行该方法) -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |